package com.thinkcube.edutainment.fragment;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.TodoAdapter;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.Todo;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.data.TodoData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment
public class TDoListFragment extends Fragment {

	View mRoot;
	private String mTag;
	private CourseData course;

	@ViewById(R.id.courselistView)
	ListView list;

	@Bean
	TodoAdapter todoAdapter;

	@Bean
	CourseDto courseDto;

	@RestService
	UserClient userClient;

	@Bean
	OAuthDto oAuthDto;

	@Trace
	@AfterViews
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setTokenUi(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	void setTokenUi(OAuth requestToken) {

		doBackground(requestToken);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mRoot = inflater.inflate(R.layout.to_do_list_fragment, null);

		if (getArguments().containsKey("tab_id")) {
			mTag = getArguments().getString("tab_id");

			course = getArguments().getParcelable("course");

		}
		return mRoot;
	}

	@Pref
	SPreferences_ pref;

	@Background
	void doBackground(OAuth requestToken) {

		courseDto.setAuthToken(requestToken.getoAuth().getToken());

		courseDto.setUserId(pref.getId().get());
		courseDto.setCourseId(course.getCourseId());

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doUi(userClient.getTodo(courseDto.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

	}

	@ViewById(R.id.progressBar)
	ProgressBar progress;

	@UiThread
	void doUi(Todo todo) {

		List<TodoData> todoList = todo.getTodoDataList();
		if (todoList != null) {
			if (todoList.size() > 0) {

				todoAdapter.setTodoData(todoList);
				list.setAdapter(todoAdapter);
				
				todoAdapter.notifyDataSetChanged();

			}
		}
		progress.setVisibility(View.GONE);
	}
}
